//
//  CZEmoticonPageCell.swift
//  keyBoard
//
//  Created by 李伟鹏 on 16/5/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 一个页按钮个数 20
let CZEmoticonNumberOfPage = 20

// 一页有多少列 7
let CZEmoticonNumberOfColumn = 7

// 一页有多少行 3
let CZEmoticonNumberOfRow = 3

protocol CZEmoticonPageCellDelegate: NSObjectProtocol{
    // 点击删除按钮
    func emoticonPageCellDidClickDeleteButton()
    
    // 点击表情按钮
    func emoticonPageCellDidClickEmoticonButton(emoticonModel: CZEmoticonModel)
}

class CZEmoticonPageCell: UICollectionViewCell {
    
    weak var delegate:CZEmoticonPageCellDelegate?
    
    var pageEmoticon: [CZEmoticonModel]?{
        didSet{
            for button in emoticonButtons{
                button.hidden = true
            }
            
            var index = 0
            for emotionModel in pageEmoticon!{
                let butoon = emoticonButtons[index]
                butoon.emoticonModel = emotionModel
                butoon.hidden = false
                index++
            }
        }
    }
    
    var indexPath: NSIndexPath?{
        didSet {
            // 设置调试信息
            debugLabel.text = "第 \(indexPath!.section) 组, 第 \(indexPath!.item) 个 cell"
            recentLabel.hidden = (indexPath!.section != 0)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var emoticonButtons = [CZEmoticonButton]()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    private func prepareUI(){
         addEmoticonButtons()
        contentView.addSubview(recentLabel)
        contentView.addSubview(deleteButton)
         contentView.addSubview(debugLabel)
    }
    
    private func addEmoticonButtons(){
        for _ in 0..<CZEmoticonNumberOfPage{
            let button = CZEmoticonButton()
            button.addTarget(self, action: "didClickEmoticonButton:", forControlEvents: UIControlEvents.TouchUpInside)
             button.titleLabel?.font = UIFont.systemFontOfSize(32)
            contentView.addSubview(button)
            emoticonButtons.append(button)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let marginLR = 5
        let marginBottom = 25
        let width = (self.bounds.width - 2 * CGFloat(marginLR)) / CGFloat(CZEmoticonNumberOfColumn)
        let height = (self.bounds.height - CGFloat(marginBottom)) / CGFloat(CZEmoticonNumberOfRow)
        var index = 0
        for button in emoticonButtons{
            let row = index / CZEmoticonNumberOfColumn
            let column = index % CZEmoticonNumberOfColumn
            let x = CGFloat(marginLR) + CGFloat(column) * width
            let y = CGFloat(row) * height
            let frame = CGRect(x: x, y: y, width: width, height: height)
            button.frame = frame
            index++
        }
        
        let deleteX = CGFloat(marginLR) + CGFloat(CZEmoticonNumberOfColumn - 1) * width
        let deleteY = CGFloat(CZEmoticonNumberOfRow - 1) * height
        let deleteFrame = CGRect(x: deleteX, y: deleteY, width: width, height: height)
        deleteButton.frame = deleteFrame
        
        recentLabel.center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height - recentLabel.frame.height)
    }
    
    @objc private func didClickDeleteButton(){
        delegate?.emoticonPageCellDidClickDeleteButton()
    }
    
    @objc private func didClickEmoticonButton(emoticonButton: CZEmoticonButton){
        delegate?.emoticonPageCellDidClickEmoticonButton(emoticonButton.emoticonModel!)
    }
    
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(30)
        label.frame = CGRect(x: 0, y: 30, width: 300, height: 100)
        label.hidden = true
        return label
    }()

    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置图片
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    private lazy var recentLabel:UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.lightGrayColor()
        label.text = "最近使用表情"
        label.sizeToFit()
        return label
    }()
}
